package org.csu.jpetstore.service.impl;

import org.csu.jpetstore.service.TokenService;
import org.csu.jpetstore.util.MyToken;
import org.csu.jpetstore.util.TokenUtil;
import org.springframework.stereotype.Service;

@Service
public class TokenServiceImpl implements TokenService {
    /**
     * token与userId匹配
     *
     * @param username 用户名
     * @param token    MyToken
     * @return boolean 是否匹配
     * @author huangzhangyan
     * @date 2022/4/13 20:42
     */
    @Override
    public boolean isUserIdEqualToken(String username, MyToken token) {
        String equalUserId = TokenUtil.getUserIdByToken(token.getToken());
        return equalUserId != null && equalUserId.equals(username);
    }

    /**
     * 创建token
     *
     * @param username 用户名
     * @return MyToken
     * @author huangzhangyan
     * @date 2022/4/13 20:43
     */
    @Override
    public MyToken createToken(String username) {
        return new MyToken(TokenUtil.createToken(username));
    }
}
